package view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

/**
 * Created by guolchen on 2014/11/18.
 */
public class AnimationImageViewEx extends ImageView {
    @SuppressWarnings("unused")
    MulitPointTouchListenerEx mulitPointTouchListener = null;
    private Matrix   oldMatrix;
    private Matrix   newMatrix;
    private Matrix   currentMatrix;
    private boolean enableAnimation = false;
    private long    animationStartTime;
    private long    animationEndTime;
    private int     animationTime;
    private float float_epsilon = 1e-3f;

    public AnimationImageViewEx(final Context context, AttributeSet attrs) {
        super(context, attrs);
        mulitPointTouchListener = new MulitPointTouchListenerEx();
        this.setOnTouchListener(mulitPointTouchListener);
    }

    public void setClickCallBack(OnZoomImageClickCallBack zoomImageClickCallBack) {
        if(null != mulitPointTouchListener) {
            mulitPointTouchListener.setZoomImageClickCallBack(zoomImageClickCallBack);
        }
    }
    
    public void EnableAnimation(Matrix oldmatrix, Matrix newmatrix, int animationtime) {
        oldMatrix = oldmatrix;
        newMatrix = newmatrix;
        enableAnimation = true;
        animationStartTime = AnimationUtils.currentAnimationTimeMillis();
        animationEndTime = animationStartTime + animationtime;
        animationTime = animationtime;
        invalidate();
    }
    public void DisableAnimation() {
        enableAnimation = false;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Drawable drawable = getDrawable();
        if (drawable == null) {
            return;
        }

        Rect bounds = drawable.getBounds();
        int w = bounds.right - bounds.left;
        int h = bounds.bottom - bounds.top;

        if (w == 0 || h == 0) {
            return;
        }
        currentMatrix = this.getImageMatrix();
        float[] values = new float[9];
        currentMatrix.getValues(values);
        if(enableAnimation){
            float[] oldvalues = new float[9];
            oldMatrix.getValues(oldvalues);
            float[] newvalues = new float[9];
            newMatrix.getValues(newvalues);
            long time = AnimationUtils.currentAnimationTimeMillis();
            if (time < animationEndTime) {
                int deltaTime = (int) (time - animationStartTime);
                float scaleratio = (float)deltaTime / (float)animationTime;
                values[0] = oldvalues[0] + scaleratio * (newvalues[0] - oldvalues[0]);
                values[2] = oldvalues[2] + scaleratio * (newvalues[2] - oldvalues[2]);
                values[4] = oldvalues[4] + scaleratio * (newvalues[4] - oldvalues[4]);
                values[5] = oldvalues[5] + scaleratio * (newvalues[5] - oldvalues[5]);

                currentMatrix.setValues(values);
                invalidate();
            } else {
                currentMatrix = newMatrix;
                enableAnimation = false;
            }
            this.setImageMatrix(currentMatrix);
            currentMatrix.getValues(values);
        }
        int saveCount = canvas.getSaveCount();
        canvas.translate(values[2], values[5]);
        canvas.scale(values[0], values[4]);
        drawable.draw(canvas);
        canvas.restoreToCount(saveCount);
    }
}